import { createResource, createBehavior } from '@inbiz/core';
import {
  createInbizSchema,
  createComponentStyleSchema,
  createDecoratorStyleSchema,
  createControllerSchema,
} from '@inbiz/setters';
import { ProcessManagement as locale } from './locale';
import defaultOption from './components/defaultOption';
import hover from './components/hover';
import ProcessGroupSelector from '../../ProcessMonitoring/designer/components/ProcessGroupSelector';

export const defaultDecoratorProps = {
  style: { height: '100%', padding: '10px 10px 10px 10px' },
};

export const defaultProps = {
  pagesNum: 20,
  allowClear: true,
  toolbar: ['refresh', 'add', 'import', 'export', 'move', 'copy', 'release', 'search'],
  inline: ['compile', 'designFlow', 'setVariable', 'releaseOrOff', 'closeOrdel'],
  processGroup: true,
  ScopeProcess: 'all',
  processName: true,
  exposureConfig: {
    ownership: false,
  },
};

export const Schema = createInbizSchema({
  attribute: {
    base: {
      type: 'void',
      'x-component': 'CollapseItem',
      properties: {
        'x-display': {
          type: 'string',
          enum: ['visible', 'hidden'],
          'x-decorator': 'FormItem',
          'x-component': 'Select',
          'x-component-props': {
            defaultValue: 'visible',
          },
        },
        'x-component-props': {
          type: 'object',
          properties: {
            pagesNum: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'Select',
              enum: [10, 20, 30, 50, 100],
            },
            toolbar: {
              type: 'array',
              'x-decorator': 'FormItem',
              'x-component': 'Checkbox.Group',
              enum: ['refresh', 'add', 'import', 'export', 'move', 'copy', 'release', 'search'],
            },
            inline: {
              type: 'array',
              'x-decorator': 'FormItem',
              'x-component': 'Checkbox.Group',
              enum: ['compile', 'designFlow', 'setVariable', 'releaseOrOff', 'closeOrdel'],
            },
            processGroup: {
              type: 'boolean',
              'x-decorator': 'FormItem',
              'x-component': 'Switch',
            },
            // 流程范围
            ScopeProcess: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': 'Select',
              enum: ['all', 'app', 'group'],
              'x-reactions': {
                dependencies: ['.processGroup'],
                fulfill: {
                  schema: {
                    'x-hidden': '{{$deps[0] !=true }}',
                  },
                },
              },
            },
            processGroupSelector: {
              type: 'string',
              'x-decorator': 'FormItem',
              'x-component': ProcessGroupSelector,
              'x-reactions': {
                dependencies: ['.processGroup', '.ScopeProcess'],
                when: "{{$deps[0] == true && $deps[1] == 'group'}}",
                fulfill: {
                  state: {
                    visible: true,
                  },
                },
                otherwise: {
                  state: {
                    visible: false,
                  },
                },
              },
            },

            processName: {
              type: 'boolean',
              'x-decorator': 'FormItem',
              'x-component': hover,
              'x-component-props': {
                tooltip: '配置是否显示分组名称',
                label: 'ProcessManagement.processName',
              },
            },
            processType: {
              type: 'boolean',
              'x-decorator': 'FormItem',
              'x-component': 'Switch',
            },
            defaultOption: {
              type: 'array',
              'x-decorator': 'FormItem',
              'x-component': defaultOption,
              'x-reactions': {
                dependencies: ['.processType'],
                fulfill: {
                  schema: {
                    'x-hidden': '{{$deps[0] !=true }}',
                  },
                },
              },
            },
            exposureConfig: {
              type: 'object',
              'x-component': 'CollapseItem',
              properties: {
                ownership: {
                  type: 'boolean',
                  'x-decorator': 'FormItem',
                  'x-component': hover,
                  'x-component-props': {
                    tooltip: '流程业务扩展,开启后新增流程来源列',
                    label: 'ProcessManagement.ownership',
                  },
                },
              },
            },
          },
        },
      },
    },
  },
  style: {
    ...createComponentStyleSchema(),
    ...createDecoratorStyleSchema(),
  },
  controller: createControllerSchema({
    events: [
      {
        name: "onSave",
        label: "新建/修改保存",
        platform: 'web'
      },
      {
        name: "onDelete",
        label: "删除",
        platform: 'web'
      },
      {
        name: "onSaveAndPublish",
        label: "新建/修改保存并发布",
        platform: 'web'
      },
      {
        name: "onSelectGroup",
        label: "流程分组选中",
        platform: 'web'
      },
      {
        name: "onExtendsSave",
        label: "自定义新建流程保存请求接口",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "formatTableData",
        label: "列表数据格式化",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "onloadH5Filter",
        label: "H5页面组件过滤",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "onloadPcFilter",
        label: "PC页面组件过滤",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "onParams",
        label: "列表请求参数格式化",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "onRequest",
        label: "自定义列表接口",
        isConfigEvent: true,
        platform: 'web'
      },
      {
        name: "onData",
        label: "列表请求返回数据格式化",
        isConfigEvent: true,
        platform: 'web'
      },
    ]
  })
});

export const Resource = createResource({
  icon: 'ProcessManagement',
  elements: [
    {
      componentName: 'Field',
      props: {
        type: 'string',
        title: {
          value: 'ProcessManagement.title',
          status: true,
        },
        'x-decorator': 'Container',
        'x-component': 'ProcessManagement',
        'x-component-props': defaultProps,
        'x-decorator-props': defaultDecoratorProps,
      },
    },
  ],
});

export const Behavior = createBehavior({
  name: 'ProcessManagement',
  extends: ['Field'],
  selector: (node: any) => node.props['x-component'] === 'ProcessManagement',
  designerProps: { propsSchema: Schema },
  designerLocales: locale,
});
